﻿using xxpl1.Log;

namespace xxpl1.Utils
{
    public class Statistic
    {
        private static int parsingCount;
        private static int downloadCount;
        private static int totalRequest;

        private static double avgParsingTime;
        private static double avgDownloadTime;
        private static int cacheHit;

        private static readonly ILog logger = LogManager.GetLogger(typeof(Statistic));

        public static double AvgParsingTime
        {
            get { return avgParsingTime; }
        }

        public static double AvgDownloadTime
        {
            get { return avgDownloadTime; }
        }

        public static string CacheHit
        {
            get { return cacheHit + "/" + totalRequest; }
        }

        public static void AddParsingTime(int elapsedMs)
        {
            avgParsingTime = ((avgParsingTime * parsingCount) + elapsedMs) / (parsingCount + 1);

            parsingCount++;

            if (parsingCount % 100 == 0)
                logger.InfoFormat("Parsing Avg. = {0}ms", avgParsingTime);
        }

        public static void AddDownLoadTime(int elapsedMs)
        {
            avgDownloadTime = ((avgDownloadTime * downloadCount) + elapsedMs) / (downloadCount + 1);

            downloadCount++;

            if (downloadCount % 100 == 0)
                logger.InfoFormat("DownLoad Avg. = {0}ms", avgDownloadTime);
        }

        public static void AddCacheHit(bool result)
        {
            totalRequest++;
            if (result) cacheHit++;
        }
    }
}